﻿#include "util/tc_option.h"
#include "util/tc_file.h"
#include "util/tc_common.h"
#include "tars2cpp.h"

using namespace std;

void usage() {
    cout << "Usage : tars2cpp [OPTION] tarsfile" << endl;
    cout << "  --dir=DIRECTORY               generate source file to DIRECTORY(create tars protocol file to DIRECTORY, default is current directory)" << endl;
    cout << "  --check-default=<true,false>  optional field with default value not do package(default: true)" << endl;
    cout << "  --unjson                      not json interface" << endl;
    cout << "  --os                          only create struct(not create interface) " << endl;
    cout << "  --include=\"dir1;dir2;dir3\"  set search path of tars protocol" << endl;
    cout << "  --tarsMaster                  create get registry info interface" << endl;
    cout << "  --currentPriority             use current path first" << endl;
    cout << "  --sinc=\"[include1;include2;include3]\"  add self define include" << endl;
    cout << "  --mark                        use tars change mark" << endl;
    cout << "  tars2cpp support type: bool byte short int long float double vector map" << endl;
    exit(0);
}

void check(vector<string>& vTars) {
    for (size_t i  = 0; i < vTars.size(); i++) {
        string ext  = tars::TC_File::extractFileExt(vTars[i]);
        if (ext == "tars") {
            if (!tars::TC_File::isFileExist(vTars[i])) {
                cerr << "file '" << vTars[i] << "' not exists" << endl;
                usage();
                exit(0);
            }
        } else {
            cerr << "only support tars file." << endl;
            exit(0);
        }
    }
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        usage();
    }

    tars::TC_Option option;
    option.decode(argc, argv);
    auto vTars = option.getSingle();

    check(vTars);
    if (option.hasParam("help")) {
        usage();
    }

    Tars2Cpp t2c;
    if (option.hasParam("dir")) {
        t2c.setBaseDir(option.getValue("dir"));
    } else {
        t2c.setBaseDir(".");
    }

    t2c.setCheckDefault(tars::TC_Common::lower(option.getValue("check-default")) == "false" ? false : true);

    t2c.setOnlyStruct(option.hasParam("os"));

    //默认支持json
    t2c.setJsonSupport(true);

    if (option.hasParam("unjson")) {
        t2c.setJsonSupport(false);
    }

    if (option.hasParam("mark")) {
        t2c.setMarkSupport(true);
    }

    if (option.hasParam("sinc")) {
        vector<string> vIncs;
        auto ssinc = tars::TC_Common::trim(option.getValue("sinc"));
        ssinc = tars::TC_Common::trimleft(tars::TC_Common::trimright(ssinc, "]"), "[");
        if (!ssinc.empty()) {
            vIncs = tars::TC_Common::sepstr<string>(ssinc, ";", false);
        }
        t2c.setSIncSupport(vIncs);
    }

    t2c.setTarsMaster(option.hasParam("tarsMaster"));

    try {
        //增加include搜索路径
        g_parse->addIncludePath(option.getValue("include"));

        //是否可以以tars开头
        g_parse->setTars(option.hasParam("with-tars"));
        g_parse->setHeader(option.getValue("header"));
        g_parse->setCurrentPriority(option.hasParam("currentPriority"));

        for (size_t i = 0; i < vTars.size(); i++) {
            g_parse->parse(vTars[i]);
            t2c.createFile(vTars[i]);//, vCoder);
        }
    } catch (exception& e) {
        cerr << e.what() << endl;
    }

    return 0;
}

